﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using MethodWorx.CMS.Administration;
using MethodWorx.CMS.UI.Areas.Dashboard.Models;
using MethodWorx.CMS.UI.Models;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using System.IO;

namespace MethodWorx.CMS.UI.Controllers
{
    public class HostedStatisticsController : Controller
    {
        private HostedApplicationService service;
        private HostedStatisticsModel model;
        //
        // GET: /HostedStatistics/
        public HostedStatisticsController(HostedApplicationService service)
        {
            this.service = service;
            this.model = new HostedStatisticsModel(this.service);
        }

        public ActionResult Index()
        {
            return View(this.model);
        }

        public ActionResult PieChart()
        {
            Response.Expires = -1;

            Chart chart = new Chart();
            chart.BackColor = Color.Transparent;
            chart.Width = Unit.Pixel(425);
            chart.Height = Unit.Pixel(200);

            chart.Legends.Add(new Legend("Legend"));
            //chart.Legends["Legend"].DockedToChartArea = "Default";


            Series series1 = new Series("Series1");
            series1.IsVisibleInLegend = true;
            series1.Legend = "Legend";
            series1.ChartArea = "ca1";
            series1.ChartType = SeriesChartType.Pie;
            series1.Font = new Font("Tahoma", 8.25f, FontStyle.Regular);
            series1.Points.Add(new DataPoint
            {
                AxisLabel = "Content (" + ((double)this.model.Usage.ContentSize / 1024 / 1024).ToString("N2") + "mb)",
                YValues = new double[] { (double)this.model.Usage.ContentSize / 1024 / 1024 }
            });
            series1.Points.Add(new DataPoint
            {
                AxisLabel = "Schema (" + ((double)this.model.Usage.SchemaSize / 1024 / 1024).ToString("N2") + "mb)",
                YValues = new double[] { (double)this.model.Usage.SchemaSize / 1024 / 1024 }
            });
            series1.Points.Add(new DataPoint
            {
                AxisLabel = "Plugin Data (" + ((double)this.model.Usage.SchemaSize / 1024 / 1024).ToString("N2") + "mb)",
                YValues = new double[] { (double)this.model.Usage.PluginData / 1024 / 1024 }
            });
            series1.Points.Add(new DataPoint
            {
                AxisLabel = "Deleted Items (" + ((double)this.model.Usage.DeletedItems / 1024 / 1024).ToString("N2") + "mb)",
                YValues = new double[] { (double)this.model.Usage.DeletedItems / 1024 / 1024 }
            });
            if (this.model.Usage.TotalInMegabytes < model.MaximumSpace)
            {
                series1.Points.Add(new DataPoint
                {
                    AxisLabel = "Unused Space (" + ((double)(this.model.MaximumSpace - this.model.Usage.TotalInMegabytes)).ToString("N2") + "mb)",
                    YValues = new double[] { (double)(this.model.MaximumSpace - this.model.Usage.TotalInMegabytes) }
                });
            }
            chart.Series.Add(series1);

            ChartArea ca1 = new ChartArea("ca1");
            ca1.BackColor = Color.Transparent;
            ca1.Area3DStyle = new ChartArea3DStyle() { Enable3D = true };
            chart.ChartAreas.Add(ca1);

            for (var i = 0; i < chart.Series.Count; i++)
            {
                for (var j = 0; j < chart.Series[i].Points.Count; j++)
                {
                    //if (!chart.Series[i].Points[j].AxisLabel.StartsWith("Unused Space"))
                    chart.Series[i].Points[j]["PieLabelStyle"] = "Disabled";
                }
            }


            using (var ms = new MemoryStream())
            {
                chart.SaveImage(ms, ChartImageFormat.Png);
                ms.Seek(0, SeekOrigin.Begin);

                return File(ms.ToArray(), "image/png", "mychart.png");
            }
        }


    }
}
